package com.jonluo.common.utils;

import com.google.code.kaptcha.Constants;
import com.jonluo.common.exception.RReturnException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

/**
 * Shiro工具类
 * @author com.jonluo
 * Date 2017/9/15
 */
public class ShiroUtils {

	public static Session getSession() {
		return SecurityUtils.getSubject().getSession();
	}
	
	public static void setSessionAttribute(Object key, Object value) {
		getSession().setAttribute(key, value);
	}

	public static Object getSessionAttribute(Object key) {
		return getSession().getAttribute(key);
	}
	
	public static String getKaptcha() {
		Object kaptcha = getSessionAttribute(Constants.KAPTCHA_SESSION_KEY);
		if(kaptcha == null){
			throw new RReturnException("验证码已失效");
		}
		getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
		return kaptcha.toString();
	}

	public static void setKaptcha(Object value) {
		setSessionAttribute(Constants.KAPTCHA_SESSION_KEY,value);
	}
}
